package com.xhg.controller;


import java.util.Arrays;
import java.util.List;

import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Page;
import com.xhg.common.PageNavation;
import com.xhg.model.Power;


public class PowerController extends BaseController<Power>{
	
	private Power powerService=Power.dao;
	
	/**
	 * 查询全部权限菜单页面
	 */
	public void all() {
		setAttr("powerCode", "power");//权限代码
		renderJsp("all.jsp");
	}
	/**
	 * 查询权限菜单集合JSON
	 */
	public void getData(){
		PageNavation page=new PageNavation();
		paramMap=getParamMap();
		if(paramMap.get("search")!=null){page.setSearch(paramMap.get("search").toString());;}//搜索关键字
		if(paramMap.get("sort")!=null){page.setSort(paramMap.get("sort").toString());}//排序字段
		if(paramMap.get("order")!=null){page.setOrder(paramMap.get("order").toString());}//排序方式
		page.setPageSize(Integer.parseInt(paramMap.get("limit").toString()));//获取每页显示数量
		page.setPageNumber(Integer.parseInt(paramMap.get("offset").toString())/page.getPageSize()+1);//设置页码
		Page<Power> powerPage=powerService.queryList(page);//获取集合对象
		StringBuilder sb=new StringBuilder();
		sb.append("{\"total\":"+powerPage.getTotalRow()+",");
		sb.append("\"rows\":"+JsonKit.toJson(powerPage.getList())+"}");
		renderJson(sb.toString());
	}
    /**
     * 跳转到添加页面
     */
    public void toAdd(){
        renderJsp("toAdd.jsp");
    }
    /**
     * 添加
     */
    public void add(){
    	Power power=getModel(Power.class,"power");
    	String[] position=getParaValues("power.position");
    	if(position!=null){
    		power.set("position",Arrays.asList(position).toString());
    	}
        Boolean b=powerService.save(power);
        if(b){
        	renderText("succeed");
        }else{
        	renderText("保存失败");
        }
    }
    /**
     * 跳转到修改页面
     */
    public void toUpd(){
    	Long id=Long.parseLong(getPara("id"));//获取修改对象的id
        setAttr("power",powerService.queryById(id));
        render("toUpd.jsp");
    }
    /**
     * 修改
     */
    public void upd() {
    	Power power=getModel(Power.class,"power");
    	String[] position=getParaValues("power.position");
    	if(position!=null){
    		power.set("position",Arrays.asList(position).toString());
    	}
        Boolean b=powerService.update(power);
        if(b){
        	renderText("succeed");
        }else{
        	renderText("修改失败");
        }
    }
    /**
     * 跳转到复制页面
     */
    public void toCopy(){
    	Long id=Long.parseLong(getPara("id"));//获取修改对象的id
        setAttr("power",powerService.queryById(id));
        render("toCopy.jsp");
    }
    /**
     * 复制
     */
    public void copy() {
    	Power power=getModel(Power.class,"power");
    	String[] position=getParaValues("power.position");
    	if(position!=null){
    		power.set("position",Arrays.asList(position).toString());
    	}
        Boolean b=powerService.save(power);
        if(b){
        	renderText("succeed");
        }else{
        	renderText("修改失败");
        }
    }
    /**
     * 删除
     */
    public void del(){
      String ids=getPara("ids");
      Boolean b=powerService.deleteByIds(ids);
      if(b){
      	renderText("succeed");
      }else{
      	renderText("修改失败");
      }
    }
    /**
     * 跳转到查看页面
     */
    public void view(){
    	Long id=Long.parseLong(getPara("id"));//获取powerId
        setAttr("power",powerService.queryById(id));
        render("view.jsp");
    }
    /**
     * zTree异步获取数据
     */
    public void getDatazTree(){
    	Long id=0L;//获取父节点id
    	if(getPara("id")!=null){
    		id=Long.parseLong(getPara("id"));
    	}
    	List<Power> pList=powerService.queryListByFid(id);
    	renderJson(JsonKit.toJson(pList));
    }
    /**
     * 验证编码是否已经存在
     */
    public void isExistPowerCode(){
    	paramMap=getParamMap();
    	Boolean b=powerService.isExistPowerCode(paramMap);
    	renderJson(!b);
    }
    public static void main(String[] args) {
		System.out.println("========");
		String[] strs={"aaaa","bbbb","cccc"};
		String s=Arrays.asList(strs).toString();
		System.out.println(s);
		char[]   data={'a','b','c'};   
		String  s2=new   String(data);
		System.out.println(s2);
	}
}
